package com.moguo.guoaiagent.demo.invoke;

/**
 * @program: guo-ai-agent
 * @description: Htpp方式调用Ai
 * @author: moguo
 * @create: 2025-04-27 15:59
 **/

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

public class HttpAiInvoke {

    public static void main(String[] args) {
        // API密钥（从环境变量或配置获取）
        String apiKey = TestApiKey.API_KEY; // 替换为实际的API密钥

        // 构建请求体JSON
        JSONObject requestBody = JSONUtil.createObj()
                .set("model", "qwen-plus")
                .set("messages", JSONUtil.createArray()
                        .put(JSONUtil.createObj()
                                .set("role", "system")
                                .set("content", "You are a helpful assistant."))
                        .put(JSONUtil.createObj()
                                .set("role", "user")
                                .set("content", "你是谁？")));

        // 发送请求
        HttpResponse response = HttpRequest.post("https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions")
                .header("Authorization", "Bearer " + apiKey)
                .header("Content-Type", "application/json")
                .body(requestBody.toString())
                .execute();

        // 处理响应
        if (response.isOk()) {
            String result = response.body();
            System.out.println("响应结果：" + result);
        } else {
            System.err.println("请求失败，状态码：" + response.getStatus());
            System.err.println("错误信息：" + response.body());
        }
    }
}

